// pages/attendance/dayCheck/dayCheck.js

const app = getApp()
const networkUtil=app.globalData.networkUtil
const util=app.globalData.util

const logStrPrefix = "attendance.dayReport"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //查询条件列表
        conditionList: [
			{ title: "迟到/早退" },
            { title: "旷工" },
            { title: "上/下班未打卡" }
        ],

        //开始日期
		startTime: util.addDay_toDate(new Date(), -6),
		endTime: util.getCurrentDate(),
        dayReportList: [],

        //分页参数
        pageNum: 1,
        pageSize: 10,
        loadMoreText: "加载更多~",
        noMoreText: "没有更多了哦~",
        totalPages: 1,
        //是否已全部加载
        noMore: false,


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this

    },

    /**
     * 获取选择的日期
     */
    bindDateChange: function(event) {
        let tagId = event.currentTarget.dataset.name
        let tagValue = event.detail.value

        switch (tagId) {
            //考勤日期
            case "startTime":{
                this.setData({
                    startTime: tagValue
                })
                break
            }
            case "endTime":{
                this.setData({
                    endTime: tagValue
                })
                break
            }
        }
    },

    /**
     * 获取用户选择的查询条件。
     */
    checkboxChange_condition: function(event) {
        let conditionList = this.data.conditionList
        let selectValues = event.detail.value

        for (let i = 0; i < conditionList.length; i++) {
            let condition = conditionList[i]
            let title = condition.title
            //默认将condition.checked置为false
            condition.checked = false

            //若用户选择了该选项
            for (let j = 0; j < selectValues.length; j++) {
                if (title == selectValues[j]) {
                    condition.checked = true
                    break
                }
            }
        }

        this.setData({
            conditionList: conditionList
        })
    },

    /**
     * 获取第一页的日考勤记录。
     */
    getDayReport: function() {
        /**
         * 获取"查询条件集合对应的字符串"。
         */
        let that = this
        let conditionStr = that.getConditionStr()

        /**
         * 获取第一页的日考勤记录。
         */
        //将页数(pageNum)置为1
        that.setData({
            pageNum: 1
        })

        let url = app.globalData.baseUrl + "/attendance/getDayReport"
        let requestData = {
            startTime: that.data.startTime,
            endTime: that.data.endTime,
            conditions: conditionStr,
            pageNum: that.data.pageNum,
            pageSize: that.data.pageSize
        }

        networkUtil.request_get(url, requestData).then((res) => {
            //若已获取日考勤信息
            that.setData({
                dayReportList: res.data.records,
                pageNum: res.data.current,
                pageSize: res.data.size,
                totalPages: res.data.pages,
                noMore: (res.data.current >= res.data.pages) ? true : false
            })
			//若that.data.dayReportList.length为0
			if(that.data.dayReportList.length==0){
				util.showToast("未找到对应的日考勤信息!")
			}
        }).catch((res) => {
            //若未获取日考勤信息
            console.error(logStrPrefix + ".getDayAttendance() 获取日考勤信息，发生错误!", res)
            util.showToast("请选择起止日期..")
        })
    },

    /**
     * 根据用户选择的查询条件集合，获取对应条件字符串。
     */
    getConditionStr: function() {
        //产生conditionStr
        let that = this
        let conditionList = that.data.conditionList
        let list_titleSelect = []

        for (let i = 0; i < conditionList.length; i++) {
            let condition = conditionList[i]
            if (condition.checked == true) {
                list_titleSelect.push(condition.title)
            }
        }

        let conditionStr = list_titleSelect.join()
        return conditionStr
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数。
     * 获取下一页日考勤记录。
     */
    onReachBottom: function() {
        //分析是否已全部加载
        let that = this
        if (that.data.pageNum >= that.data.totalPages) {
            that.setData({
                noMore: true
            })
            return
        }

        /**
         * 若没有全部加载，加载下一页数据。
         */
        //获取"查询条件集合对应的字符串"。
        let conditionStr = that.getConditionStr()
        let url = app.globalData.baseUrl + "/attendance/getDayReport"
        let requestData = {
            startTime: that.data.startTime,
            endTime: that.data.endTime,
            conditions: conditionStr,
            pageNum: that.data.pageNum + 1,
            pageSize: that.data.pageSize
        }

        networkUtil.request_get(url, requestData).then((res) => {
            //若已获取下一页日考勤信息
            that.setData({
                dayReportList: that.data.dayReportList.concat(res.data.records),
                pageNum: res.data.current,
                pageSize: res.data.size,
                totalPages: res.data.pages,
                noMore: (res.data.current >= res.data.pages) ? true : false
            })
        }).catch((res) => {
            //若未获取下一页日考勤信息
            console.error(logStrPrefix + " 获取下一页日考勤信息，发生错误!", res)
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})